#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2021/6/15 
# @Author  : Mik
import pytest
from api.user_management_interface import UserMange
from common.logger import logger
from common.mysql_operate import my_db


@pytest.fixture(scope="session")
def login_fixture():
    username = 'wintest'
    password = '123456'
    header = {
        "Content-Type": "application/x-www-form-urlencoded"
    }
    # payload = {
    #     "username": username,
    #     "password": password
    # }
    loginInfo = UserMange().login(username=username, password=password)
    yield loginInfo.response.json()


@pytest.fixture(scope="function")
def insert_delete_user():
    """删除用户前，先在数据库插入一条用户数据"""
    insert_sql = "INSERT INTO user(username, password, role, sex, telephone, address) VALUES" \
                 "('测试test', '123456', '1', '1', '13488888888', '北京市海淀区')"
    my_db.execute_db(insert_sql)
    logger.info("删除用户操作：插入新用户--准备用于删除用户")
    logger.info("执行前置SQL：{}".format(insert_sql))
    yield
    # 因为有些情况是不给删除管理员用户的，这种情况需要手动清理上面插入的数据
    # del_sql = base_data["init_sql"]["insert_delete_user"][1]
    # db.execute_db(del_sql)
    # step_last()
    # logger.info("删除用户操作：手工清理处理失败的数据")
    # logger.info("执行后置SQL：{}".format(del_sql))


@pytest.fixture(scope="function")
def delete_register_user():
    """注册用户前，先删除数据，用例执行之后，再次删除以清理数据"""
    del_sql = "DELETE FROM user WHERE username = '测试test'"
    my_db.execute_db(del_sql)

    logger.info("注册用户操作：清理用户--准备注册新用户")
    logger.info("执行前置SQL：{}".format(del_sql))
    yield
    my_db.execute_db(del_sql)

    logger.info("注册用户操作：删除注册的用户")
    logger.info("执行后置SQL：{}".format(del_sql))
